## 76ms 5720KB

from collections import deque
num,k=[int(i) for i in input().split()]

named={}
for i in range(num):
    sget = input().split()
    named[sget[0]]=int(sget[1])

num,end_row=divmod(num,k)
each_row=num
num+=end_row

res=sorted(named.items(),key=lambda  x:x[0],reverse=True) 
res=sorted(res,key=lambda x:x[1]) #list tuple

    
for row in range(k): 
    line=deque()
    line.append(res.pop()[0])

    for col in range(num-1):
        if col%2==0:
            line.appendleft(res.pop()[0])
        else:
            line.append(res.pop()[0])
    
    num=each_row
    print(*line,sep=" ")





